# coding:utf-8
from ext import Flask, JSONEncoder
from utils import log_file_handler
from .route import MyRoutes
from models import Model


routes = MyRoutes()
model = Model()


def create_app(config, db, migrate):
    """
    @options: 配置项
    @db: db实例
    @migrate: 数据库迁移命令实例 [注意：必须配置环境变量FLASK_APP, 或者项目启动文件是 app.py | wsgi.py, 才能够使用 flask db init 等
    """

    # 创建Flask应用实例
    app = Flask(__name__)

    # 根据配置文件配置应用
    app.config.from_object(config)

    # 重写默认JSON编码器
    app.json_encoder = JSONEncoder

    # 绑定日志模块
    logger_handler = log_file_handler(config.PROJECT_NAME, config.LOG_LEVEL)
    app.logger.addHandler(logger_handler)

    # 绑定数据库模块
    db.init_app(app)
    migrate.init_app(app, db)

    # 注册模型
    model.init_app(db)

    # 注册路由
    routes.init_app(app)

    return app

if __name__ == '__main__':
    pass
